﻿<UserControl x:Class="MVVM.Views.ListNames"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:customCommand="clr-namespace:MVVM.Commands"
             xmlns:nameValidator="clr-namespace:MVVM.Validation"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <customCommand:CustomCommand x:Key="MyCustomCommand" />

        <Style TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <WrapPanel>
                            <Border BorderBrush="Red" BorderThickness="2" VerticalAlignment="Top">
                                <AdornedElementPlaceholder/>
                            </Border>
                            <TextBlock Foreground="Red" FontSize="25" Margin="5,0,5,0">!</TextBlock>
                        </WrapPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={RelativeSource Self}, 
                            Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="ValidationExampleSecond" TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <WrapPanel>
                            <Border BorderBrush="Green" BorderThickness="2" VerticalAlignment="Top">
                                <AdornedElementPlaceholder/>
                            </Border>
                            <TextBlock Foreground="Green" FontSize="25" Margin="5,0,5,0">!</TextBlock>
                        </WrapPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={RelativeSource Self}, 
                            Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>

        <Style x:Key="ValidationExampleThird" TargetType="{x:Type TextBox}">
            <Setter Property="Validation.ErrorTemplate">
                <Setter.Value>
                    <ControlTemplate>
                        <StackPanel>
                            <StackPanel Orientation="Horizontal">
                                <Image Height="16" Margin="0,0,5,0" Source="../Images/warning.png"/>
                                <AdornedElementPlaceholder x:Name="Holder"/>
                            </StackPanel>
                            <Label Foreground="Red" Content="{Binding ElementName=Holder, 
                                Path=AdornedElement.(Validation.Errors)[0].ErrorContent}"/>
                        </StackPanel>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={RelativeSource Self}, 
                            Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </UserControl.Resources>

    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" MinWidth="200" />
            <ColumnDefinition Width="Auto" MinWidth="150" />
            <ColumnDefinition Width="Auto" MinWidth="150" />
            <ColumnDefinition Width="Auto" MinWidth="150" />
        </Grid.ColumnDefinitions>
        <ListBox Grid.Column="0" x:Name="listBoxListNames" ItemsSource="{Binding NamesList}" />
        <StackPanel Grid.Column="1" Orientation="Vertical" Margin="10">
            <TextBox x:Name="textBoxName" Margin="5" />
            <Button x:Name="buttonAddName" Content="Add Name" Click="buttonAddName_Click" Margin="5" />
            <Button x:Name="buttonDeleteName" Content="Delete Name" Click="buttonDeleteName_Click" Margin="5" />
        </StackPanel>
        <StackPanel Grid.Column="2" Margin="10">
            <TextBox x:Name="textBoxChangeValues" Margin="5" >
                <TextBox.Text>
                    <Binding Path="NameForValidation" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <nameValidator:NameValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
            <Button x:Name="buttonChangeValues" Content="Add Name" Command="{Binding Add}" Margin="5"/>
            <Button x:Name="buttonExecuteCustomCommand" Content="Custom Add Command" 
                    Command="{Binding AddCustomCommand}"
                    CommandParameter="{Binding ElementName=textBoxChangeValues, Path=Text}" Margin="5">
            </Button>
        </StackPanel>
        <StackPanel Grid.Column="3" Margin="10">
            <TextBox x:Name="textBoxValidationExampleSecond" Margin="5" Style="{StaticResource ValidationExampleSecond}">
                <TextBox.Text>
                    <Binding Path="NameForValidation" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <nameValidator:NameValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>

            <TextBox x:Name="textBoxValidationExampleThird" Margin="5" Style="{StaticResource ValidationExampleThird}">
                <TextBox.Text>
                    <Binding Path="NameForValidation" UpdateSourceTrigger="PropertyChanged">
                        <Binding.ValidationRules>
                            <nameValidator:NameValidationRule />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox.Text>
            </TextBox>
        </StackPanel>
    </Grid>
</UserControl>
